package com.djinfo.work.util;

import java.io.Serializable;
import java.util.List;

@SuppressWarnings("unused")

public class PageSet<T> implements Serializable {
	private static final long serialVersionUID = 1L;
	private List<T> list; // 要返回的当前查询页的记录列表
	private int allRow; // 总记录数
	private int totalPage; // 总页数
	private int currentPage = 1; // 当前页
	private int numPerPage; // 每页记录数pagesize
	

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public int getAllRow() {
		return allRow;
	}

	/** 数据库中该记录的总条数 */
	public void setAllRow(int allRow) {
		this.allRow = allRow;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	public int getNextPage() {

		int nextPage = currentPage + 1;
		if (nextPage >= totalPage) {
			return currentPage;
		} else {
			return nextPage;
		}
	}

	public int getPreviousPage() {

		int previousPage = currentPage - 1;
		if (previousPage <= 0) {
			return currentPage;
		} else {
			return previousPage;
		}
	}




	/**
	 * 计算总页数,静态方法,供外部直接通过类名调用
	 * 
	 * @param pageSize
	 *            每页记录数
	 * @param allRow
	 *            总记录数
	 * @return 总页数
	 */
	public static int countTotalPage(final int pageSize, final int allRow) {
		int totalPage;
		if (allRow != 0) {
			totalPage = allRow % pageSize == 0 ? allRow / pageSize : allRow / pageSize + 1;
		} else {
			totalPage = 1;
		}
		return totalPage;
	}

	/**
	 * 计算当前页开始记录
	 * 
	 * @param pageSize
	 *            每页记录数
	 * @param currentPage
	 *            当前第几页
	 * @return 当前页起始记录号的偏移量
	 */
	public static int countOffset(final int pageSize, final int currentPage) {
		final int offset = pageSize * (currentPage - 1);
		return offset;
	}

	/**
	 * 计算当前页,若为0或者请求的URL中没有"?page=",则用1代替
	 * 
	 * @param page
	 *            传入的参数(可能为空,即0,则返回1)
	 * @return 当前页
	 */
	public static int countCurrentPage(int page) {
		final int curPage = (page == 0 ? 1 : page);
		return curPage;
	}

}
